package com.dm0858.bianmin.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;

import com.bumptech.glide.Glide;
import com.dm0858.bianmin.R;
import com.dm0858.bianmin.app.MyApp;
import com.dm0858.bianmin.databinding.ActivityMain2LayoutBinding;
import com.dm0858.bianmin.mine.activity.BindPhoneActivity;
import com.dm0858.bianmin.mine.base.BaseActivity;
import com.dm0858.bianmin.mine.fragment.MineFragment;
import com.dm0858.bianmin.mine.listener.IResultListener;
import com.dm0858.bianmin.mine.model.MineModel;
import com.dm0858.bianmin.mine.network.Retrofit;
import com.dm0858.bianmin.mine.tools.DialogUtils;
import com.dm0858.bianmin.mine.tools.LogUtils;
import com.dm0858.bianmin.mine.tools.SPUtils;
import com.dm0858.bianmin.mine.tools.ToastUtils;
import com.dm0858.bianmin.ui.fragment.HomeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import butterknife.ButterKnife;

/**
 * @description 主界面
 * @author 迪迪丶Sir
 * @e-mail asd969709719@qq.com
 * @since 2020-05-01
 **/
public class Main2LayoutActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private ActivityMain2LayoutBinding binding;

    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;
    private HomeFragment homeFragment;
    private MineFragment mineFragment;

    private Retrofit retrofit;

    //导航栏被选中时的id，用于判断是否被选中
    private int itemId;
    //上一次导航栏被选中的id,用于判断是否还停留在当前页面
    private int lastItemId = R.id.btn_home;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main2_layout);
        binding.setOnClick(this);
        initData();
    }

    @Override
    protected void initData() {
        if (retrofit == null) retrofit = new Retrofit();
        fragmentManager = getSupportFragmentManager();
        binding.radioGroup.setOnCheckedChangeListener(this);
        binding.radioGroup.check(R.id.btn_home);
        HttpIndustry industry = new HttpIndustry();
        industry.genParams();
        FinalHttp finalHttp = new FinalHttp();
        finalHttp.post(industry.getFuncName(), industry,
                new AjaxCallBack<Object>() {
                    @Override
                    public void onSuccess(Object t) {
                        super.onSuccess(t);
                        com.dm0858.bianmin.ui.activity.CacheUtils.setIndustry(Main2LayoutActivity.this,t.toString());
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo,
                                          String strMsg) {
                    }
                });
    }

    /**
     * 调起系统发短信功能
     * @param phoneNumber
     * @param message
     */
    public void doSendSMSTo(String phoneNumber,String message){
        if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));
            intent.putExtra("sms_body", message);
            startActivity(intent);
        }
    }

    /**
     * 调用拨号界面
     * @param phone 电话号码
     */
    private void call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        transaction = fragmentManager.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        itemId = i;
        if (itemId == R.id.btn_home || itemId == R.id.btn_mine) hideFragment(transaction);
        switch (i){
            case R.id.btn_home:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.fl_content, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case R.id.btn_advisory:
                break;
            case R.id.btn_add:

                break;
            case R.id.btn_message:
                break;
            case R.id.btn_mine:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    transaction.add(R.id.fl_content, mineFragment);
                } else {
                    transaction.show(mineFragment);
                }
                break;
        }
        if (lastItemId != itemId) lastItemId = itemId;
        transaction.commit();
    }

    /**
     * @description 隐藏Fragment
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-25
     **/
    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null && itemId != R.id.btn_home) {
            transaction.hide(homeFragment);
        }

        if (mineFragment != null && itemId != R.id.mine) {
            transaction.hide(mineFragment);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.txt_advisory:
                binding.radioGroup.check(R.id.btn_advisory);
                call("08314088880");
                break;
            case R.id.txt_add:
                switch (MyApp.launchMode){
                    case -1:
                        ToastUtils.showShort("您还未登录");
                        break;
                    case 1:
                        //手机号登录
                        MineModel.checkPhone(retrofit, MyApp.userid, MyApp.phone, new MineModel.IBindPhoneListener() {
                            @Override
                            public void bind() {
                                startActivity(AgreementActivity.class);
                            }

                            @Override
                            public void unBind() {

                            }
                        });
                        break;
                    case 2:
                        //微信登录
                        MineModel.checkPhone(retrofit, MyApp.openid, new MineModel.IBindPhoneListener() {
                            @Override
                            public void bind() {
                                startActivity(AgreementActivity.class);
                            }

                            @Override
                            public void unBind() {
                                DialogUtils.showTipDialog(Main2LayoutActivity.this, "绑定手机号", "您的账户需要绑定手机后才能发布帖子，是否需要绑定手机？", new DialogUtils.onClickListener() {
                                    @Override
                                    public void negativeClick() {

                                    }

                                    @Override
                                    public void positiveClick() {
                                        Bundle b = new Bundle();
                                        b.putString("openid", MyApp.openid);
                                        Intent intent = new Intent(Main2LayoutActivity.this,BindPhoneActivity.class);
                                        intent.putExtras(b);
                                        startActivity(intent);
                                    }

                                    @Override
                                    public void cancelClick() {

                                    }

                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                });
                            }
                        });
                        break;
                }
                break;
            case R.id.txt_message:
                binding.radioGroup.check(R.id.btn_message);
                doSendSMSTo("13309095565","");
                break;
        }
    }
}